<?php
class user {
	protected $user_id;
	protected $userDetail;
	protected $groups;
	protected $MyGroup;
	
	public function __construct($obj = null)
	{
		$this->user_id = 0;
		if (is_null($obj))
		{
			$session = new session();
			if ($session->get('registred_user'))
				$this->user_id = $session->get('registred_user');
		}
		elseif (is_object($obj) && get_class($obj) == 'SQLRow')
		{
			$this->user_id = $obj->user_id;
		}
		elseif (is_numeric($obj))
		{
			$this->user_id = $obj;
		}
		elseif (is_string($obj))
		{
			$users = new SQL ('user');
			$found = $users
			->where(array('pseudo_clean' => $this->clean($obj)))
			->select()
			->current();
			
			if ($found)
				$this->user_id = $found->user_id;
			else
				throw new ArgError('User not found');
		}
		if ($this->GetDetails() === false)
			throw new ArgError('User not found');
		$this->LoadGroups();
	}
	
	protected function clean($username)
	{
		$username = strtolower($username);
		$username = str_replace(array('é', 'è', 'ê', 'ë'), 'e', $username);
		$username = str_replace(array('à', 'â', 'ä'), 'a', $username);
		$username = str_replace(array('î', 'ï'), 'i', $username);
		
		return $username;
	}
	
	public function GetId()
	{
		return $this->user_id;
	}
	
	public function GetGroup()
	{
		return $this->groups;
	}
	
	public function isRegistred()
	{
		return $this->userDetail->registred == 'yes' 
		&& $this->userDetail->active == 'yes';
	}
	
	public function __get($var)
	{
		return $this->userDetail->__get($var);
	}

	public function __set($var, $value)
	{
		$this->userDetail->__set($var, $value);
	}
	
	protected function GetDetails()
	{
		$table = new SQL('user');
		$user = $table
			->where(array('user_id' => $this->user_id))
			->select();
		if ($user->count() != 1)
			return false;
		$this->userDetail = $user->current();
		return true;
	}
	
	protected function LoadGroups()
	{
		$table = new SQL(array('UG' => 'user_group', 'G' => 'group'));
		$this->groups = array();
		$groups = $table
			->where(array(
				'user_group' =>
					array(
						'child_id' => $this->user_id,
						'child_type' => 'user',
					),
				'group' =>
					array(
						'group_id' => array('field' => 'UG.group_id'),
					),
			))
			->select();
		foreach ($groups as $group)
		{
			$this->groups[$group->group_id] = array(
				'id' => $group->group_id,
				'name' => $group->group_name,
				'moderator' => $group->moderator == 'yes',
				'color' => $group->group_color,
				'type' => $group->group_type,
				'mapply' => $group->group_apply_moderator == 'yes',
				'description' => $group->group_description,
				'primary' => true,
			);
			if ($group->default == 'yes')
				$this->MyGroup = $group->group_id;
			
			$this->subGroups($group->group_id);
		}
	}
	
	protected function subGroups($child)
	{
		$table = new SQL(array('UG' => 'user_group', 'G' => 'group'));
		$groups = $table
			->where(array(
				'user_group' =>
					array(
						'child_id' => $child,
						'child_type' => 'group',
					),
				'group' =>
					array(
						'group_id' => array('field' => 'UG.group_id'),
					),
			))
			->select();
		foreach ($groups as $group)
		{
			if (!isset($group->group_id))
			{
				$this->groups[$group->group_id] = array(
					'id' => $group->group_id,
					'name' => $group->group_name,
					'moderator' => $group->moderator == 'yes',
					'color' => $group->group_color,
					'type' => $group->group_type,
					'mapply' => $group->group_apply_moderator == 'yes',
					'description' => $group->group_description,
					'primary' => false,
				);
				$this->subGroups($group->group_id);
			}
		}
	}
	
	public function inGroup($group_id)
	{
		return isset($this->groups[$group_id]);
	}
	
	public function defaultGroup()
	{
		return $this->groups[$this->MyGroup];
	}
	
	public function ValidPseudo($pseudo)
	{
		$table = new SQL('user');
		$user = $table
			->where(array('pseudo_clean' => strtolower($pseudo)))
			->select();
		if ($user->count() > 0)
		{
			return 'err_pseudo';
		}
		else
			return true;
	}
	
	public function InscriptionForm ($translate)
	{
		return new form(array(
			array('name' => 'user_pseudo', 'label' => 'pseudo_field', 'valid' => 'callback', 'callback' => array($this, 'ValidPseudo')),
			array('name' => 'user_mail', 'label' => 'email_field', 'valid' => 'email'),
		), $translate);
	}

	public function create($vars)
	{
		$table = new SQL('user');
		return $table->insert(array(
			'pseudo' => $vars['user_pseudo'],
			'pseudo_clean' => strtolower($vars['user_pseudo']),
			'mail' => $vars['user_mail'],
		));
	}
	
}
